package com.nanyang.tms.manager.waybill.impl;

import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.waybill.WaybillIncomeFeeDao;
import com.nanyang.tms.domain.waybill.WaybillIncomeFeeQuery;
import com.nanyang.tms.domain.waybill.WaybillIncomeFeeVO;
import com.nanyang.tms.manager.waybill.WaybillIncomeFeeManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component("waybillIncomeFeeManager")
public class WaybillIncomeFeeManagerImpl implements WaybillIncomeFeeManager {
    @Autowired
    private WaybillIncomeFeeDao waybillIncomeFeeDao;
    @Override
    public PageBean<WaybillIncomeFeeVO> listWaybillIncomeFee(WaybillIncomeFeeQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = waybillIncomeFeeDao.countIncomeFee(query);
            PageBean<WaybillIncomeFeeVO> page = new PageTableBean<WaybillIncomeFeeVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(waybillIncomeFeeDao.listIncomeFee(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
}
